package com.dxf.common.utils;

import com.dxf.common.exception.BaseErrorInfoInterface;
import com.dxf.common.exception.CommonEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 统一返回结果，， return this;实现链式编程   .add().add().add()
 */
@Data
public class ResultJson {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private String code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    //把构造方法私有
    private ResultJson() {}

    //成功静态方法
    public static ResultJson ok() {
        ResultJson r = new ResultJson();
        r.setSuccess(true);
        r.setCode(CommonEnum.SUCCESS.getResultCode());
        r.setMessage(CommonEnum.SUCCESS.getResultMsg());
        return r;
    }

    //失败静态方法
    public static ResultJson error() {
        ResultJson r = new ResultJson();
        r.setSuccess(false);
        r.setCode(CommonEnum.ERROR.getResultCode());
        r.setMessage(CommonEnum.ERROR.getResultMsg());
        return r;
    }

    /**
     * 失败
     */
    public static ResultJson error(String code, String message) {
        ResultJson rb = new ResultJson();
        rb.setSuccess(false);
        rb.setCode(code);
        rb.setMessage(message);
        return rb;
    }


    /**
     * 异常错误失败
     */
    public static ResultJson error(BaseErrorInfoInterface errorInfo) {
        ResultJson rb = new ResultJson();
        rb.setSuccess(false);
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        return rb;
    }



    public ResultJson success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public ResultJson message(String message){
        this.setMessage(message);
        return this;
    }

    public ResultJson code(String code){
        this.setCode(code);
        return this;
    }

    public ResultJson data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public ResultJson data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
